package Communication;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Exceptions.ConnectionFailedException;
import Protocolo.MiniTCP;
import Protocolo.Consts;

public class Cliente {

	public static void main(String[] args) throws Exception {
		FileInputStream fis = new FileInputStream(new File("C:\\Users\\Gustavo Stor\\Downloads\\American.Idol.S11E40.HDTV.x264-2HD.mp4"));
		MiniTCP socket = new MiniTCP(5000, InetAddress.getByName("172.20.4.176"));
		
		socket.connect();

		OutputStream outBuffer = socket.getOutputStream();

		Grafico chart = new Grafico(socket, 0);
		JFrame frame = new JFrame("CWND");
		frame.getContentPane().add(chart.montarGrafico());
		frame.setSize(400,300);
		frame.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						System.exit(0);
					}
				}
				);
		frame.setVisible(true);
		
		/*JPanel j = new JPanel();
		j.add(chart.montarGrafico());
		j.setSize(400, 300);
		
		JFrame frame = new JFrame("CWND");
		frame.getContentPane().add();
		frame.setSize(400,300);
		frame.setVisible(true);*/
		
		
		//j.getCo

		long time1 = System.currentTimeMillis();
		while(fis.available() > 0) {
			if (socket.getOutputStream().size() < Consts._MAX_BUFFER_SIZE) {
				byte[] lol = new byte[Math.min(Consts._MSS, fis.available())];
				fis.read(lol, 0, Math.min(Consts._MSS, fis.available()));
				outBuffer.write(lol);
			}
		}
		System.out.println((double)(System.currentTimeMillis()-time1)/1000 + " segundos.");
		System.out.println("Terminou!");
		socket.finish();
	}
}